package com.example.shirotest.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.shirotest.base.ResultVo;
import com.example.shirotest.entry.Role;
import com.example.shirotest.entry.TableList;
import com.example.shirotest.entry.User;
import com.example.shirotest.service.UserService;
import com.example.shirotest.utils.MD5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;

import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("/admin")
public class AdminController extends BashController{

    @Autowired
    private UserService userService;

    @RequiresRoles({"admin"})
    @RequestMapping("/index")
    public @ResponseBody String index(){
        return "admin index";
    }


    @RequiresRoles({"admin"})
    @RequestMapping("/user")
    public String user(Model model){
        setUserInfo(model);
        model.addAttribute("tag_mb","admin-user");

        return "admin/user";
    }

    @RequiresRoles({"admin"})
    @RequestMapping("/userlist")
    @ResponseBody
    public TableList userlist(Integer pagenum,Integer pagesize){
        Page<User> page = new Page<>(pagenum,pagesize);
        IPage<User> userIPage = userService.selectPageVo(page);

        TableList<User> userTableList = new TableList<>();
        userTableList.setList(userIPage.getRecords());
        userTableList.setTotal((int) userIPage.getTotal());

        return userTableList;
    }

    @RequiresRoles({"admin"})
    @RequestMapping("/userlistby")
    @ResponseBody
    public TableList userlistby(Integer pagenum,Integer pagesize,String sort,String order){
        PageInfo<User> userPageInfo = userService.selectByPage(pagenum, pagesize, sort + " " + order);

        //封装输出数据
        TableList<User> userTableList = new TableList<>();
        userTableList.setList(userPageInfo.getList());
        userTableList.setTotal((int) userPageInfo.getTotal());

        return userTableList;
    }


    //重置登录密码
    @RequiresRoles({"admin"})
    @RequestMapping("/repwd")
    @ResponseBody
    public ResultVo repwd(Integer id) throws NoSuchAlgorithmException {
        //生成六位随机密码数
        Random random = new Random();
        Integer r = random.nextInt(1000,9999);
        String encrypt = MD5Util.encrypt(r.toString());

        userService.updateUserPwd(id,encrypt);

        return new  ResultVo(1,"密码成功："+r,"");
    }


    //修改账号状态
    @RequiresRoles({"admin"})
    @RequestMapping("/locked")
    @ResponseBody
    public ResultVo locked(Integer id,Integer locked){
        if(userService.updateUserLocked(id, locked)){
            return new  ResultVo(1,"操作成功","");
        }

        return new  ResultVo(0,"操作失败","");
    }

}
